//
//  KDWeiboCell.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/11/26.
//  Copyright © 2015年 魏强. All rights reserved.
//

#import "KDWeiboCell.h"
#import "KDWeiboCellFrame.h"
#import "UIImageView+WebCache.h"
#import "KDWeiboPhotosView.h"
#import "KDWeiboToolbar.h"
#import "KDUser.h"
#import "KDPhoto.h"
#import "KDWeibo.h"
#import "KDIconView.h"
@interface KDWeiboCell ()<KDWeiboToolbarDelegate>
/** 原创微博整体 */
@property (nonatomic, weak) UIView *originalView;
/** 头像 */
@property (nonatomic, weak) KDIconView *iconView;
/** 配图 */
@property (nonatomic, weak) KDWeiboPhotosView *photosView;
/** 昵称 */
@property (nonatomic, weak) UILabel *nameLabel;
/** 时间 */
@property (nonatomic, weak) UILabel *timeLabel;
/** 来源 */
@property (nonatomic, weak) UILabel *sourceLabel;
/** 正文 */
@property (nonatomic, weak) UILabel *contentLabel;

/** 工具条 */
@property (nonatomic, weak) KDWeiboToolbar *toolbar;
@end
@implementation KDWeiboCell

- (void)awakeFromNib {
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView andVersion:(NSInteger)version
{
    static NSString *ID = @"weiboCell";
    KDWeiboCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//    cell.version = version;
    if (!cell) {
        cell = [[KDWeiboCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID andVersion:version];
    }
    return cell;
}
/**
 *  cell的初始化方法，一个cell只会调用一次
 */
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier andVersion:(NSInteger)version
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        // 点击cell的时候不要变色
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
//         设置选中时的背景为蓝色
//                UIView *bg = [[UIView alloc] init];
//                bg.backgroundColor = [UIColor blueColor];
//                self.selectedBackgroundView = bg;
//        
        // 这个做法不行
        //        self.selectedBackgroundView.backgroundColor = [UIColor blueColor];
        
        // 初始化原创微博
        [self setupOriginal];
        
        
        // 初始化工具条
        [self setupToolbarWithVersion:version];
    }
    return self;
}

/**
 * 初始化工具条
 */
- (void)setupToolbarWithVersion:(NSInteger)version
{
    KDWeiboToolbar *toolbar = [KDWeiboToolbar toolbarWithVersion:version];
    toolbar.delegate = self;
    toolbar.backgroundColor = [UIColor whiteColor];
    toolbar.layer.borderWidth = 0.3;
    toolbar.layer.borderColor = GBColor(224, 232, 231,0.47).CGColor;
    [self.contentView addSubview:toolbar];
    self.toolbar = toolbar;
}
/**
 * 初始化原创微博
 */
- (void)setupOriginal
{
    /** 微博整体 */
    UIView *originalView = [[UIView alloc] init];
    originalView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:originalView];
    self.originalView = originalView;
    
    /** 头像 */
    KDIconView *iconView = [[KDIconView alloc] init];
    [originalView addSubview:iconView];
    self.iconView.layer.borderWidth = 2.0f;
    self.iconView.layer.borderColor = [UIColor whiteColor].CGColor;
    self.iconView = iconView;
    
    /** 配图 */
    KDWeiboPhotosView *photosView = [[KDWeiboPhotosView alloc] init];
    [originalView addSubview:photosView];
    self.photosView = photosView;
    
    /** 昵称 */
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = KDWeiboCellNameFont;
    nameLabel.textColor = [UIColor orangeColor];
    [originalView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    /** 时间 */
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = KDWeiboCellTimeFont;
    timeLabel.textColor = [UIColor lightGrayColor];
    [originalView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    /** 来源 */
    UILabel *sourceLabel = [[UILabel alloc] init];
    sourceLabel.font = KDWeiboCellSourceFont;
    sourceLabel.textColor = [UIColor lightGrayColor];
    [originalView addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;
    
    /** 正文 */
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.font = KDWeiboCellContentFont;
    contentLabel.numberOfLines = 0;
    [originalView addSubview:contentLabel];
    self.contentLabel = contentLabel;
}
- (void)setWeiboCellFrame:(KDWeiboCellFrame *)weiboCellFrame{
    _weiboCellFrame = weiboCellFrame;
    
    KDWeibo *weibo = weiboCellFrame.weibo;
    /** 微博整体 */
    self.originalView.frame = weiboCellFrame.originalViewF;
    
    /** 头像 */
    self.iconView.frame = weiboCellFrame.iconViewF;
    self.iconView.layer.cornerRadius = self.iconView.frame.size.width / 2;
    self.iconView.clipsToBounds = YES;
    self.iconView.profile_image_url = weibo.profile_image_url;
//    self.iconView.user = user;
    
    /** 配图 */
    if (weibo.pic_ids.count) {
        self.photosView.frame = weiboCellFrame.photosViewF;
        self.photosView.photos = weibo.pic_ids;
        self.photosView.hidden = NO;
    } else {
        self.photosView.hidden = YES;
    }
    
    /** 昵称 */
    self.nameLabel.text = weibo.uname;
    self.nameLabel.frame = weiboCellFrame.nameLabelF;
    
    /** 时间 */
    self.timeLabel.frame = weiboCellFrame.timeLabelF;
    self.timeLabel.text = weibo.created_at;
    
    /** 来源 */
    self.sourceLabel.frame = weiboCellFrame.sourceLabelF;
    self.sourceLabel.text = weibo.source;
    
    /** 正文 */
    self.contentLabel.text = weibo.text;
    self.contentLabel.frame = weiboCellFrame.contentLabelF;
    
    /** 工具条 */
    self.toolbar.frame = weiboCellFrame.toolbarF;
    self.toolbar.weibo = weibo;
}

#pragma mark - KDWeiboToolbarDelegate
- (void)ReportBtnClickWithWeiboTool{
    if (_delegate && [_delegate respondsToSelector:@selector(reportBtnClickWithCell:)]) {
        [_delegate reportBtnClickWithCell:self];
    }
}

@end
